﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Show
{
    /// <summary>
    /// 预约单详细信息
    /// </summary>
    public partial class ShowBookingBillDetail : UserControl, IView
    {
        Promise _promise = new Promise();
        Guid _bookingBillId;
        public ShowBookingBillDetail()
        {
            InitializeComponent();
        }

        public ShowBookingBillDetail(Guid bookingBillId)
            : this()
        {
            this._bookingBillId = bookingBillId;
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection
                .AddAction(new ActionNode("close", Resources.isDelete, "关闭", OnBtnCloseClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(GetBookingBillDetail).Run();
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("预约单详细信息");
        }
        #endregion

        #region Method
        private void GetBookingBillDetail()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                SingleResult<WebRef.BookingBillDetail> result = new Service.GetService().GetBookingBillDetail(_bookingBillId);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.txtCustomerName.Text = result.Data.BookingBill.CustomerName;
                        this.txtCustomerTel.Text = result.Data.BookingBill.CustomerTelephone;
                        this.txtCarLPN.Text = result.Data.BookingBill.AutomobileLPN;
                        this.txtCarVIN.Text = result.Data.BookingBill.AutomobileVIN;
                        this.txtBrand.Text = result.Data.Automobile.BrandName;
                        this.txtMaker.Text = result.Data.Automobile.MakerName;
                        this.txtSeries.Text = result.Data.Automobile.SeriesName;
                        this.txtModel.Text = result.Data.Automobile.ModelName;
                        this.ctlGridData.DataSource = result.Data.ServiceList;
                        this.ctlEditTargetDate.Text = result.Data.BookingBill.TargetDate.ToString();
                        this.ctlEditSalesManager.Text = result.Data.BookingBill.SalesManagerName;
                        this.txtRemark.Text = result.Data.BookingBill.Memo;
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void OnBtnCloseClick(object sendr, object e)
        {
            UIContext.Current.ViewManager.CloseView(true, null);
        }
        #endregion
    }
}
